﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Bids.Core.DomainObjects;
using Bids.Data;
using Bids.Web.Model;


using Bids.Core.Common.Pager;
using Bids.Service;

namespace Bids.Web.Controllers
{
    public class HomeController : Controller
    {
        #region
        ISystemService _iSys;
        IRepository<Contribution> _iRpoContribution;
        IRepository<Task> _iRpoTask;
        IRepository<SetMeal> _iRpoSetMeal;
        IRepository<ContributionItem> _iRpoContrItem;
        IRepository<User> _iRpoUser;
        IRepository<SinglePage> _iRpoSinglePage;
        public HomeController(IRepository<Contribution> iRpoContribution
            , IRepository<Task> iRpoTask
            , IRepository<SetMeal> iRpoSetMeal
            , IRepository<ContributionItem> iRpoContrItem
            , IRepository<User> iRpoUser,
            ISystemService iSys,
            IRepository<SinglePage> iRpoSinglePage)
        {
            _iRpoContribution = iRpoContribution;
            _iRpoTask = iRpoTask;
            _iRpoSetMeal = iRpoSetMeal;
            _iRpoContrItem = iRpoContrItem;
            _iRpoUser = iRpoUser;
            _iSys = iSys;
            _iRpoSinglePage = iRpoSinglePage;
        }
        #endregion

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            model.NewTasks = _iRpoTask.FindAll().OrderByDescending(t => t.CreateTime).Take(11);
            model.Contributions = _iRpoContribution.FindAll().OrderByDescending(t => t.RankTime).Take(6);
            model.FinishedCount = _iRpoTask.Where(t => t.Status == (int)TaskStatus.已结束).Count();
            //model.SuccessCases = _iRpoTask.Where(t => t.Status == (int)TaskStatus.已结束)
            //    .OrderByDescending(t => t.FinishTime)
            //    .Take(36)
            //    .Select(t =>
            //                new SuccessCase
            //                {
            //                    ID = t.ID,
            //                    Name = t.Name,
            //                    ImageUrl = t.Contributions
            //                    .Where(d=>d.Status == (int)ContributionStatus.已截稿)
            //                    .Select(
            //                        d=>d.ContributionItems
            //                            .OrderByDescending(
            //                            r=>r.CreateTime
            //                            )
            //                            .Select(r=>r.Logo)
            //                            .FirstOrDefault()
            //                        ).FirstOrDefault()
            //                });
            ViewBag.Title = _iSys.Sys.SiteName;

            return View(model);
        }
        

        public ActionResult Logo()
        {
            var list = _iRpoContribution.Where(t => t.Status == (int)ContributionStatus.已截稿)
                .Select(t => t.ContributionItems
                    .OrderByDescending(d => d.CreateTime)
                    .FirstOrDefault()
                ).Take(32);
            return View(list);
        }
        public ActionResult Design(int? page)
        {
           var list= _iRpoUser.Where(t => t.Type == (int)UserType.设计师).OrderByDescending(t => t.Scores.Sum(d => d.ScoreValue)).ToPagedList(page ?? 1, 10);
           return View(list);
        }
        public ActionResult Evaluate(int? page)
        {
            var list = _iRpoContribution.Where(t => t.Status == (int)ContributionStatus.已截稿).OrderByDescending(t => t.RankScore).ToPagedList(page ?? 1, 10);
            return View(list);
        }
        public ActionResult SetMeal()
        {
            return View(_iRpoSetMeal.FindAll().ToList());
        }
        public ActionResult contact(int id)
        {
            return View(_iRpoSinglePage.Find(id));
        }
    }
}
